package com.ljc.webview.utils;

import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

public class PreferenceUtils {

    private static String PREFERENCE_FILE_NAME = "webView_file";
    private static Preferences preferences;
    private static DatabaseHelper databaseHelper;
    private static String KEY_TOKEN = "token";

    private static void initPreference(Context context) {
        if (databaseHelper == null) {
            databaseHelper = new DatabaseHelper(context);
        }
        if (preferences == null) {
            preferences = databaseHelper.getPreferences(PREFERENCE_FILE_NAME);
        }

    }

    public static void putToken(Context context,String value) {
        initPreference(context);
        preferences.putString(KEY_TOKEN, value);
        preferences.flush();
    }

    public static String getToken(Context context) {
        initPreference(context);
        return preferences.getString(KEY_TOKEN, "preferences 未获取到token");
    }

}